package com.poi.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;

import com.poi.entity.TableEntity;

public class LeadHandlerUtil {
	
	private static final Logger logger = LogManager.getLogger(LeadHandlerUtil.class);
	
	public static List<File> getXlsFiles(String inpath,String outPath){
		Path p = Paths.get(inpath);
		File file = p.toFile();
		File[] files = file.listFiles();
		
		
		if(files !=null && files.length !=0){
			List<File> fileList = new ArrayList<File>();
			for(File subFile:files){
				if(subFile.isFile() && subFile.getName().trim().toLowerCase().endsWith(".xls")){
					fileList.add(subFile);
				}
			}
			if(fileList.size() !=0){
				Path path = Paths.get(outPath);
				if(!path.toFile().isDirectory()){
					try {
						Files.createDirectories(path);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			if(fileList.size() == 0){
				logger.error("-------------- xlsPath路径有误或者其路径下不存在excel文件-----------------");
			}
			return fileList;
		}
		logger.error("-------------- xlsPath路径有误或者其路径下不存在excel文件-----------------");
		return null;
	}
	
	public static List<TableEntity> getNameByFile(File file){
		logger.info("--------------读取 "+file.getName()+" 文件完毕-----------------");
		return ExcelImportUtil.importExcel(file,  TableEntity.class, new ImportParams());
	}
	
	
}
